package cn.nebula.music.service;

import java.sql.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.AlbumDAO;
import cn.nebula.music.dao.AlbumSongDAO;
import cn.nebula.music.dao.CombinationAlbumDAO;
import cn.nebula.music.dao.SingerAlbumDAO;
import cn.nebula.music.dao.UserAlbumDAO;
import cn.nebula.music.domain.Album;
import cn.nebula.music.domain.AlbumSong;
import cn.nebula.music.domain.Combination;
import cn.nebula.music.domain.CombinationAlbum;
import cn.nebula.music.domain.Singer;
import cn.nebula.music.domain.SingerAlbum;
import cn.nebula.music.domain.Song;
import cn.nebula.music.domain.User;
import cn.nebula.music.domain.UserAlbum;

@Service
public class AlbumServiceImpl implements AlbumService {

	@Autowired
	private AlbumDAO albumDAO;
	@Autowired
	private UserAlbumDAO userAlbumDAO;
	@Autowired
	private AlbumSongDAO albumSongDAO;
	@Autowired
	private CombinationAlbumDAO combinationAlbumDAO;
	@Autowired
	private SingerAlbumDAO singerAlbumDAO;

	@Override
	public List<Album> findNewAlbums() throws Exception {
		return albumDAO.findNewAlbums();
	}

	@Override
	public Set<Album> findAllAlbums() throws Exception {
		return albumDAO.findAllAlbums();
	}

	@Override
	public Set<Album> findAllAlbumsOfBT() throws Exception {
		return albumDAO.findAllAlbumsOfBT();
	}

	@Override
	public Set<Album> findAllAlbumsWP() throws Exception {
		return albumDAO.findAllAlbumsWP();
	}

	@Override
	public Set<Album> findAlbumsByPublishTime(Date publishTime) throws Exception {
		return albumDAO.findAlbumsByPublishTime(publishTime);
	}

	@Override
	public Album findAlbumById(Integer id) throws Exception {
		return albumDAO.findAlbumById(id);
	}

	@Override
	public Set<Album> findAlbumsByName(String name) throws Exception {
		return albumDAO.findAlbumsByName(name);
	}

	@Override
	public Set<Album> findAlbumsByHeat(Integer heat) throws Exception {
		return albumDAO.findAlbumsByHeat(heat);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addAlbum(Album album) throws Exception {
		albumDAO.addAlbum(album);
		Set<User> users = album.getUsers();
		if (users != null) {
			for (User user : users) {
				UserAlbum ua = new UserAlbum();
				ua.setAlbum(album.getId());
				ua.setUser(user.getId());
				userAlbumDAO.addUserAlbum(ua);
			}
		}
		Set<Song> songs = album.getSongs();
		if (songs != null) {
			for (Song song : songs) {
				AlbumSong as = new AlbumSong();
				as.setAlbum(album.getId());
				as.setSong(song.getId());
				albumSongDAO.addAlbumSong(as);
			}
		}
		Set<Combination> combinations = album.getCombinations();
		if (combinations != null) {
			for (Combination combination : combinations) {
				CombinationAlbum ca = new CombinationAlbum();
				ca.setAlbum(album.getId());
				ca.setCombination(combination.getId());
				combinationAlbumDAO.addCombinationAlbum(ca);
			}
		}
		Set<Singer> singers = album.getSingers();
		if (singers != null) {
			for (Singer singer : singers) {
				SingerAlbum sa = new SingerAlbum();
				sa.setAlbum(album.getId());
				sa.setSinger(singer.getId());
				singerAlbumDAO.addSingerAlbum(sa);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateAlbum(Album album) throws Exception {
		System.out.println(album);
		albumDAO.updateAlbum(album);
		Boolean isModify = false;
		int size1 = 0;// 更改前的数量
		int size2 = 0;// 更改后的数量
		Set<User> users1 = findAlbumById(album.getId()).getUsers();
		Set<User> users2 = album.getUsers();
		if (users1 != null) {
			size1 = users1.size();
		}
		if (users2 != null) {
			size2 = users2.size();
		}
		if (size1 == size2) {
			if (!users1.toString().equals(users2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (User user : users1) {
				UserAlbum ua = new UserAlbum();
				ua.setAlbum(album.getId());
				ua.setUser(user.getId());
				userAlbumDAO.deleteUserAlbum(ua);
			}
			for (User user : users2) {
				UserAlbum ua = new UserAlbum();
				ua.setAlbum(album.getId());
				ua.setUser(user.getId());
				userAlbumDAO.addUserAlbum(ua);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Song> songs1 = findAlbumById(album.getId()).getSongs();
		Set<Song> songs2 = album.getSongs();
		if (songs1 != null) {
			size1 = songs1.size();
		}
		if (songs2 != null) {
			size2 = songs2.size();
		}
		if (size1 == size2) {
			if (!songs1.toString().equals(songs2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Song song : songs1) {
				AlbumSong as = new AlbumSong();
				as.setAlbum(album.getId());
				as.setSong(song.getId());
				albumSongDAO.deleteAlbumSong(as);
			}
			for (Song song : songs2) {
				AlbumSong as = new AlbumSong();
				as.setAlbum(album.getId());
				as.setSong(song.getId());
				albumSongDAO.addAlbumSong(as);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Combination> combinations1 = findAlbumById(album.getId()).getCombinations();
		Set<Combination> combinations2 = album.getCombinations();
		if (combinations1 != null) {
			size1 = combinations1.size();
		}
		if (combinations2 != null) {
			size2 = combinations2.size();
		}
		if (size1 == size2) {
			if (!combinations1.toString().equals(combinations2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Combination combination : combinations1) {
				CombinationAlbum ca = new CombinationAlbum();
				ca.setAlbum(album.getId());
				ca.setCombination(combination.getId());
				combinationAlbumDAO.deleteCombinationAlbum(ca);
			}
			for (Combination combination : combinations2) {
				CombinationAlbum ca = new CombinationAlbum();
				ca.setAlbum(album.getId());
				ca.setCombination(combination.getId());
				combinationAlbumDAO.addCombinationAlbum(ca);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Singer> singers1 = findAlbumById(album.getId()).getSingers();
		Set<Singer> singers2 = album.getSingers();
		if (singers1 != null) {
			size1 = singers1.size();
		}
		if (singers2 != null) {
			size2 = singers2.size();
		}
		if (size1 == size2) {
			if (!singers1.toString().equals(singers2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Singer singer : singers1) {
				SingerAlbum sa = new SingerAlbum();
				sa.setAlbum(album.getId());
				sa.setSinger(singer.getId());
				singerAlbumDAO.deleteSingerAlbum(sa);
			}
			for (Singer singer : singers2) {
				SingerAlbum sa = new SingerAlbum();
				sa.setAlbum(album.getId());
				sa.setSinger(singer.getId());
				singerAlbumDAO.addSingerAlbum(sa);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteAlbumById(Integer id) throws Exception {
		userAlbumDAO.deleteUserAlbumByAlbumId(id);
		albumSongDAO.deleteAlbumSongByAlbumId(id);
		combinationAlbumDAO.deleteCombinationAlbumByAlbumId(id);
		singerAlbumDAO.deleteSingerAlbumByAlbumId(id);
		albumDAO.deleteAlbumById(id);
	}

}
